package com.hzhr.iot.toll.constant;

import com.hzhr.iot.toll.dao.OptionsDao;
import com.hzhr.iot.toll.entity.OptionsEntity;
import com.hzhr.iot.toll.queryModel.custom.OptionsCustomQueryModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

@Component
public class TollOptions implements CommandLineRunner {
    public static class GAS_USER_TYPE {
        public static String RESIDENTIAL_USER;
        public static String COMMERCIAL_USER;
    }

    @Autowired
    OptionsDao optionsDao;

    @Override
    public void run(String... strings) throws Exception {
        OptionsCustomQueryModel queryModel = new OptionsCustomQueryModel();
        queryModel.setShow(true);
        List<OptionsEntity> optionsEntities = optionsDao.selectEntityList(queryModel);
        for (OptionsEntity optionsEntity :
                optionsEntities) {
            TollOptions deviceOptions = new TollOptions();
            Class[] classes = TollOptions.class.getDeclaredClasses();
            for (Class clazz:
                    classes) {
                if (clazz.getSimpleName().equals(optionsEntity.getTypeName())) {
                    Field[] fields = clazz.getDeclaredFields();
                    for (Field field :
                            fields) {
                        String fieldName = field.getName();
                        if (fieldName.equals(optionsEntity.getName())) {
                            field.set(deviceOptions, optionsEntity.getCode());
                        }
                    }
                }
            }
        }
    }
}
